package com.xuhuan.thread.study2;

/**
 * @author huan.xu
 * @date 2021/9/29 14:32
 * @description 停止线程
 */
public class StopThreadDemo1 implements Runnable {
    private boolean flag = true;

    @Override
    public void run() {
        int i = 0;
        while (flag) {
            System.out.println("run thread:" + i);
        }
    }

    public void stopThread() {
        this.flag = false;
    }

    public static void main(String[] args) {
        StopThreadDemo1 stopThreadDemo1 = new StopThreadDemo1();
        new Thread(stopThreadDemo1).start();

        for (int i = 0; i < 1000; i++) {
            System.out.println("main:" + i);
            if (i == 900) {
                stopThreadDemo1.stopThread();
                System.out.println("线程停止了");
            }
        }
    }
}
